﻿//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------

using System.Collections.Generic;

namespace LS.Editor.DataTableTools
{
    public sealed partial class DataTableProcessor
    {
        public abstract class GenericDataProcessor<T> : DataProcessor
        {
            public override System.Type Type
            {
                get
                {
                    return typeof(T);
                }
            }

            public override bool IsId
            {
                get
                {
                    return false;
                }
            }

            public override bool IsComment
            {
                get
                {
                    return false;
                }
            }

            public abstract T Parse(string value);
        }
        public abstract class GenericDataArrayProcessor<T>: GenericDataProcessor<T[]>
        {
            public override T[] Parse(string value)
            {
                string[] split = value.Split(';', System.StringSplitOptions.RemoveEmptyEntries);
                T[] array = new T[split.Length];
                for (int i = 0; i < split.Length; i++)
                {
                    array[i] = ParseItem(split[i]);
                }
                return array;
            }
            public abstract T ParseItem(string value);
        }
        public abstract class GenericDataListProcessor<T> : GenericDataProcessor<List<T>>
        {
            public override List<T> Parse(string value)
            {
                string[] split = value.Split(';', System.StringSplitOptions.RemoveEmptyEntries);
                List<T> list = new List<T>(split.Length);
                for (int i = 0; i < split.Length; i++)
                {
                    list.Add(ParseItem(split[i]));
                }
                return list;
            }
            public abstract T ParseItem(string value);
        }
        public abstract class GenericDataDictionaryProcessor<T> : GenericDataProcessor<Dictionary<string,T>>
        {
            public override Dictionary<string, T> Parse(string value)
            {
                string[] split = value.Split(';', System.StringSplitOptions.RemoveEmptyEntries);
                Dictionary<string, T> dic = new Dictionary<string, T>(split.Length);
                for (int i = 0; i < split.Length; i++)
                {
                    string[] keyValue = split[i].Split(':');
                    T valueItem = ParseItem(keyValue[1]);
                    dic.Add(keyValue[0], valueItem);
                }
                return dic;
            }
            public abstract T ParseItem(string value);
        }
    }
}
